package com.anolesoft.yr.mbus.prod.model.crtl;

import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.yr.mbus.prod.model.dto.ProductionTypeDTO;
import com.anolesoft.yr.mbus.prod.model.service.ProductionTypeService;
import com.anolesoft.yr.mbus.prod.model.vo.ProductionTypeVO;
import com.anolesoft.yr.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@RestController
public class ProductionTypeController {
    @Autowired
    private ProductionTypeService prodTypeServiceImpl;

    @PostMapping(value = "/api/prod/prodtype/add", produces = MediaType.APPLICATION_JSON_VALUE)//返回json数据
    public AjaxResult add(ProductionTypeVO prodTypeVO) {
        ProductionTypeDTO prodTypeDTO = vo2dto(prodTypeVO, true);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE, prodTypeServiceImpl.add(prodTypeDTO));
    }

    @PutMapping(value = "/api/prod/prodtype/update")
    public AjaxResult update(ProductionTypeVO prodTypeVO) {
        ProductionTypeDTO prodTypeDTO = vo2dto(prodTypeVO, false);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.CREATE_SUCCESS_MESSAGE,prodTypeServiceImpl.update(prodTypeDTO));
    }

    @DeleteMapping("/api/prod/prodtype/delete1/{id}")
    public AjaxResult delete(@PathVariable("id") String id) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, prodTypeServiceImpl.delete(id));
    }

    @DeleteMapping("/api/prod/prodtype/delete")
    public AjaxResult  deleteBatch(@RequestParam(value="arr[]") String[] ids) {
        return new AjaxResult(HttpStatus.SUCCESS, Constants.DELETE_SUCCESS_MESSAGE, prodTypeServiceImpl.deleteBatch(ids));
    }

    @GetMapping("/api/prod/prodtype/get/{id}")
    public AjaxResult selectOneById(@PathVariable("id") String id) {
        Optional<ProductionTypeDTO> prodTypeDTO = prodTypeServiceImpl.queryProduction(id);
        if (prodTypeDTO.isPresent()) {
            AjaxResult ajaxResult = new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, dto2vo(prodTypeDTO.get()) );
            return ajaxResult;
        } else {
            return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, null);
        }
    }

    private ProductionTypeVO dto2vo(ProductionTypeDTO prodTypeDTO) {
        return ProductionTypeVO.builder()
                .objuid(prodTypeDTO.getObjuid())
                .remark(prodTypeDTO.getRemark())
                .typeCode(prodTypeDTO.getTypeCode())
                .typeName(prodTypeDTO.getTypeName())
                .typeNature(prodTypeDTO.getTypeNature())
                .facCode(prodTypeDTO.getFacCode())
                .cTime(prodTypeDTO.getCTime())
                .cUserUid(prodTypeDTO.getCUserUid())
                .delFlag(prodTypeDTO.getDelFlag())
                .mTime(prodTypeDTO.getMTime())
                .mUserUid(prodTypeDTO.getMUserUid())
                .build();
    }


    @GetMapping("/api/prod/prodtype/list/all")
    public AjaxResult selectAll() {
        List<ProductionTypeVO> list = prodTypeServiceImpl.queryProductions().stream().map(t -> dto2vo(t)).collect(Collectors.toList());
        PageInfo<List<ProductionTypeVO>> listPageInfo = new PageInfo<>(list.size(), list);
        AjaxResult ajaxResult = new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, listPageInfo);
        return ajaxResult;
    }

    @GetMapping("/api/prod/prodtype/list/all/page")
    public AjaxResult selectPageAll(@RequestParam("rows")Integer rows,@RequestParam("page")Integer page) {
        PageData<List<ProductionTypeDTO>> listPageData = prodTypeServiceImpl.queryPageAll(rows, page);
        List<ProductionTypeVO> list = listPageData.getItems().stream().map(x -> dto2vo(x)).collect(Collectors.toList());
        PageInfo<List<ProductionTypeVO>> listPageInfo = new PageInfo<>(rows, listPageData.getTotal(), list);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, listPageInfo);
    }

    @GetMapping("/api/prod/prodtype/list/condition")
    public AjaxResult selectList(ProductionTypeVO prodTypeVO) {
        ProductionTypeDTO prodTypeDTO = vo2dto(prodTypeVO, false);
        List<ProductionTypeDTO> prodTypeDTOS = prodTypeServiceImpl.queryList(prodTypeDTO);
        List<ProductionTypeVO> list = prodTypeDTOS.stream().map(x -> dto2vo(x)).collect(Collectors.toList());
        PageInfo<List<ProductionTypeVO>> listPageInfo = new PageInfo<>(list.size(), list);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, listPageInfo);
    }

    @GetMapping("/api/prod/prodtype/list/condition/page")///api/prod/prodtype/list/condition/page
    public AjaxResult selectListPage(ProductionTypeVO prodTypeVO) {
        ProductionTypeDTO prodTypeDTO = vo2dto(prodTypeVO, false);
        Integer rows = prodTypeVO.getRows();
        Integer page = prodTypeVO.getPage();
        PageData<List<ProductionTypeDTO>> pageData = prodTypeServiceImpl.queryListPage(prodTypeDTO, rows, page);
        List<ProductionTypeVO> list = pageData.getItems().stream().map(x -> dto2vo(x)).collect(Collectors.toList());
        PageInfo<List<ProductionTypeVO>> listPageInfo = new PageInfo<>(rows, pageData.getTotal(), list);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, listPageInfo);
    }

    @PostMapping("/api/prod/prodtype/list/combox")
    public AjaxResult selectComboxList(@RequestBody ProductionTypeVO prodTypeVO) {
        ProductionTypeDTO prodTypeDTO = vo2dto(prodTypeVO, false);
        Integer rows = prodTypeVO.getRows();
        Integer page = prodTypeVO.getPage();
        PageData<List<ProductionTypeDTO>> pageData = prodTypeServiceImpl.queryComboxList(prodTypeDTO, rows, page);
        List<ProductionTypeVO> list = pageData.getItems().stream().map(x -> dto2vo(x)).collect(Collectors.toList());
        PageInfo<List<ProductionTypeVO>> listPageInfo = new PageInfo<>(rows, pageData.getTotal(), list);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE, listPageInfo);
    }

    private ProductionTypeDTO vo2dto(ProductionTypeVO production,Boolean dID) {
        return ProductionTypeDTO.builder()
                .objuid(dID ? CommonUtils.fastUUID() : production.getObjuid())
                .remark(production.getRemark())
                .typeCode(production.getTypeCode())
                .typeName(production.getTypeName())
                .typeNature(production.getTypeNature())
                .facCode(production.getFacCode())
                .cTime(dID ? LocalDateTime.now() : production.getCTime())
                .cUserUid(production.getCUserUid())
                .delFlag(production.getDelFlag())
                .mTime( dID ? production.getMTime() : LocalDateTime.now())
                .mUserUid(production.getMUserUid())
                .build();
    }

}
